package com.jsy.common.util;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.name.Rename;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class PictureTool {
    public static void main(String[] args) throws IOException {
        try {
            URL url = PictureTool.class.getClassLoader().getResource("conf.property");
            File file = new File(url.getFile());
        } catch (Exception e) {
            e.printStackTrace();
        }
        create();
        create1();
        create2();
        create3();
        create4();
        create5();
        create6();
        create7();
    }
//Create a thumbnail from an image file
    public static void create() throws java.io.IOException {
        Thumbnails.of(new File("images/original.jpg"))
                .size(160, 160)
                .toFile(new File("thumbnail.jpg"));
    }
//Create a thumbnail with rotation and a watermark
    public static void create1() throws java.io.IOException {
        Thumbnails.of(new File("images/original.jpg"))
                .size(160, 160)
                .rotate(90)
                .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.jpg")), 0.5f)
                .outputQuality(0.8)
                .toFile(new File("image-with-watermark.jpg"));
    }
//Create a thumbnail and write to an OutputStream
    public static void create2() throws java.io.IOException {
        Thumbnails.of(new File("images/original.jpg"))
                .size(160, 160)
                .toFile(new File("thumbnail.jpg"));
    }
 //   Creating fixed-size thumbnails
 public static void create3() throws java.io.IOException {
     BufferedImage originalImage = ImageIO.read(new File("images/original.jpg"));

     BufferedImage thumbnail = Thumbnails.of(originalImage)
             .size(200, 200)
             .asBufferedImage();
 }
//  Scaling an image by a given factor
public static void create4() throws java.io.IOException {
    BufferedImage originalImage = ImageIO.read(new File("images/original.jpg"));

    BufferedImage thumbnail = Thumbnails.of(originalImage)
            .scale(0.25)
            .asBufferedImage();
}
//Rotating an image when creating a thumbnail
public static void create5() throws java.io.IOException {
    BufferedImage originalImage = ImageIO.read(new File("images/original.jpg"));

    BufferedImage thumbnail = Thumbnails.of(originalImage)
            .size(200, 200)
            .rotate(90)
            .asBufferedImage();
}
    //Creating a thumbnail with a watermark
    public static void create6() throws java.io.IOException {
        BufferedImage originalImage = ImageIO.read(new File("images/original.jpg"));
        BufferedImage watermarkImage = ImageIO.read(new File("images/watermark.jpg"));

        BufferedImage thumbnail = Thumbnails.of(originalImage)
                .size(200, 200)
                .watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f)
                .asBufferedImage();
    }
    //Writing thumbnails to a specific directory
    public static void create7() throws java.io.IOException {
        File destinationDir = new File("images");

        Thumbnails.of("images/apple.jpg", "images/banana.jpg", "images/cherry.jpg")
                .size(200, 200)
                .toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);

    }


}
